package com.lp.biz.order.model.vo;
import com.lp.common.annotation.ExcelColumn;
import lombok.Data;

import java.math.BigDecimal;
import java.time.format.DateTimeFormatter;

import static com.lp.common.constant.OrderConstant.*;
import static com.lp.common.utils.Tools.checkOrderTitle;
import static com.lp.common.utils.Tools.stripTrailingZeros;

/**
 * @program: backend
 * @description:
 * @author: Ke.Song
 * @since: 2024-09-21 21:37:51
 */
@Data
public class OrderExcel {

    private final static DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public OrderExcel(OrderList vo) {
        this.orderNum = vo.getOrderNum();
        this.title = checkOrderTitle(vo.getOrderType(), vo.getMembersType(), vo.getTitle());
        this.nickName = vo.getNickName();
        this.phone = vo.getPhone();
        this.actualPrice = setBigDecimal(vo.getActualPrice());
        this.payPrice = setBigDecimal(vo.getPayPrice());
        this.commissionCharge = setBigDecimal(vo.getCommissionCharge());
        this.kickbackCharge = setBigDecimal(vo.getKickbackCharge());
        this.couponCharge = setBigDecimal(vo.getCouponCharge());
        setStatus(vo.getStatus());
        setOrderType(vo.getOrderType());
        this.createAt = vo.getCreateAt().format(FORMATTER);
    }

    @ExcelColumn(head = "订单号", index = 0)
    private String orderNum;

    @ExcelColumn(head = "产品类型", index = 1)
    private String orderType;

    @ExcelColumn(head = "产品名", index = 2)
    private String title;

    @ExcelColumn(head = "购买人", index = 3)
    private String nickName;

    @ExcelColumn(head = "手机号", index = 4)
    private String phone;

    @ExcelColumn(head = "实际价格", index = 5)
    private String actualPrice;

    @ExcelColumn(head = "支付价格", index = 6)
    private String payPrice;

    @ExcelColumn(head = "状态", index = 7)
    private String status;

    @ExcelColumn(head = "手续费", index = 8)
    private String commissionCharge;

    @ExcelColumn(head = "分佣金额", index = 9)
    private String kickbackCharge;

    @ExcelColumn(head = "优惠券", index = 10)
    private String couponCharge;

    @ExcelColumn(head = "日期", index = 11)
    private String createAt;

    private void setStatus(Integer status) {
        if (status.equals(STATUS_UNPAID)) {
            this.status = "未支付";
        } else if (status.equals(STATUS_PAID)) {
            this.status = "已支付";
        } else if (status.equals(STATUS_CANCELED)) {
            this.status = "主动取消";
        } else if (status.equals(STATUS_TIME_OUT_CANCELED)) {
            this.status = "超时取消";
        } else if (status.equals(STATUS_FAIL)) {
            this.status = "支付失败";
        } else if (status.equals(STATUS_REFUND_FAIL)) {
            this.status = "退款失败";
        } else if (status.equals(STATUS_REFUND_SUCCESS)) {
            this.status = "退款成功";
        }
    }

    private void setOrderType(Integer orderType) {
        if(orderType.equals(ORDER_TYPE_COURSE)) {
            this.orderType = "课程";
        } else if(orderType.equals(ORDER_TYPE_MEMBERS)) {
            this.orderType = "芳程讲书会员";
        } else if(orderType.equals(ORDER_TYPE_READING)) {
            this.orderType = "共读会";
        } else if(orderType.equals(ORDER_TYPE_TRAINING)) {
            this.orderType = "训练营";
        }
    }

    private String setBigDecimal(BigDecimal bigDecimal) {
        BigDecimal result = stripTrailingZeros(bigDecimal);
        return result == null ? null : result.toPlainString();
    }
}
